package com.gitee.feizns.explore.data_structure.visual.frame.bubble;

import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.util.Random;

/**
 * @author feizns
 * @since 2020/1/2
 */
public class Circle {

    private boolean fill;

    private int x;
    private int y;
    private int r;

    private int sx = 10;
    private int sy = 10;

    private int xl;
    private int yl;

    public Ellipse2D toEllipse() {
        return new Ellipse2D.Float(x, y, r * 2, r * 2);
    }

    public Circle(int x, int y, int r, int xl, int yl) {
        this.x = x;
        this.y = y;
        this.r = r;
        this.xl = xl;
        this.yl = yl;
    }

    public boolean inCircle(Point point) {
        return point.x > x && point.y > y && point.x < x + (r * 2) && point.y < y + (r * 2);
    }

    public void changeFill() {
        this.fill = !fill;
    }

    public void move() {
        x += sx;
        y += sy;
        if ( x >= getXUpLimit() || x <= 0 )
            sx = -sx;
        if ( y >= getYUpLimit() || y <= 0 )
            sy = -sy;
    }

    public int getXUpLimit() {
        return xl - (r * 2);
    }

    public int getYUpLimit() {
        return yl - (r * 2) - 20;
    }

    public boolean getFill() {
        return fill;
    }

    static Random RANDOM = new Random();

    public static Circle random(View view, int r) {
        RANDOM = new Random();
        Dimension size = view.getRootPane().getContentPane().getPreferredSize();
        int x = (RANDOM.nextInt(size.width - r * 2) + r) / 10 * 10;
        int y = (RANDOM.nextInt(size.height - r * 2) + r) / 10 * 10;
        Circle circle = new Circle(x, y, r, size.width, size.height);
        return circle;
    }

}
